package com.atguigu.gmall.order.config;

import com.atguigu.gmall.common.constans.MqConstant;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author A
 * @version 1.0
 * @description: TODO
 * @date 2025/9/14 23:45
 */
@Configuration
public class OrderMqConfig {
    @Bean
    public Exchange orderExchange() {
        return new TopicExchange(MqConstant.ORDER_EVENT_EXCHANGE, true, false, null);
    }
    //延时队列
    @Bean
    public Queue orderDelayQueue() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("x-dead-letter-exchange", MqConstant.ORDER_EVENT_EXCHANGE);
        map.put("x-dead-letter-routing-key", MqConstant.ORDER_OUTTIME_ROUTING_KEY);
        map.put("x-message-ttl", 60000*10); // 10分钟
        return new Queue(MqConstant.ORDER_DELAY_QUEUE,
                true,
                false,
                false, map);
    }
    // 延时队列绑定
    @Bean
    public Binding orderDelayBinding() {
        /*
         * String destination      目的地
         * DestinationType  目的地类型
         * String exchange          交换机
         * String routingKey
         * @Nullable Map<String, Object> arguments
         **/
        return new Binding(MqConstant.ORDER_DELAY_QUEUE,
                Binding.DestinationType.QUEUE,
                MqConstant.ORDER_EVENT_EXCHANGE,
                MqConstant.ORDER_CREATE_ROUTING_KEY,
                null);
    }
    //死信队列
    @Bean
    public Queue orderDeadQueue() {
        return new Queue(MqConstant.ORDER_DEAD_QUEUE, true, false, false, null);
    }
    //死信队列绑定
    @Bean
    public Binding orderDeadBinding() {
        return new Binding(MqConstant.ORDER_DEAD_QUEUE,
                Binding.DestinationType.QUEUE,
                MqConstant.ORDER_EVENT_EXCHANGE,
                MqConstant.ORDER_OUTTIME_ROUTING_KEY,
                null);
    }
}
